package com.dz.xjd.ui.activity;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import com.dz.xjd.R;
import com.dz.xjd.api.TokenManager;
import com.dz.xjd.app.Constant;
import com.dz.xjd.params.Params;
import com.dz.xjd.ui.base.BaseActivity;
import com.dz.xjd.ui.contract.LoginContract;
import com.dz.xjd.ui.model.LoginModel;
import com.dz.xjd.ui.presenter.LoginPresenter;
import com.dz.xjd.utils.ToastUtil;
import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginPresenter, LoginModel> implements LoginContract.View {

    @BindView(R.id.layout_top_tv_left)
    TextView mTvTitleBack;

    @BindView(R.id.layout_top_tv_title)
    TextView mTvtitle;

    @BindView(R.id.et_login_username)
    EditText mEtUsername;

    @BindView(R.id.et_login_validate)
    EditText mEtValidate;

    @BindView(R.id.btn_login_validate)
    Button mBtnValidate;

    @BindView(R.id.btn_login_login)
    Button mBtnLogin;

    @BindView(R.id.cb_login_read)
    CheckBox mCbRead;

    private String username;
    private String validate;

    private TimeCount time;


    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        mTvtitle.setText("登录");
        mCbRead.setOnCheckedChangeListener(mOnCheckedChangeListener);
        time = new TimeCount(60000, 1000);
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void returnsendValidateData(int code) {
        if(code == 2){
            tokenManager.requestToken();
        }else{
            tokenManager.removeTokenChangeListener(validaTokenChange);
        }
    }

    @Override
    public void returnLoginData(int code) {
        if(code == 0){
            startActivity(MainActivity.class);
            tokenManager.removeTokenChangeListener(validaTokenChange);
        }else if(code == 2){
            tokenManager.requestToken();
        }else{
            tokenManager.removeTokenChangeListener(validaTokenChange);
        }
    }

    @OnClick({R.id.btn_login_login, R.id.btn_login_validate, R.id.layout_top_tv_left})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_top_tv_left:
                onBackPressed();
                break;

            case R.id.btn_login_validate:
                validateSubmit();
                break;

            case R.id.btn_login_login:
                loginSubmit();
                break;
        }
    }

    private void validateSubmit() {
        if (verifyUser()) {
            String header = Params.normalHeadParams(Constant.METHOD_SMS);
            String params = Params.msmParams(username);
            tokenManager.addTokenChangeListener(validaTokenChange);
            mPresenter.sendValidateRequest(header, params);
            time.start();
        }
    }

    private void loginSubmit() {
        if (verifyUser()&& verifycode()) {
            String header = Params.normalHeadParams(Constant.METHOD_LOGIN);
            String params = Params.loginParams(username, validate);
            tokenManager.addTokenChangeListener(loginTokenChange);
            mPresenter.getLoginRequest(header, params);
        }
    }

    private boolean verifyUser() {
        username = mEtUsername.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            ToastUtil.showShort("请输入用户名");
            return false;
        }
        return true;
    }

    private boolean verifycode() {
        validate = mEtValidate.getText().toString().trim();
        if (TextUtils.isEmpty(validate)) {
            ToastUtil.showShort("请输入验证码");
            return false;
        }
        return true;
    }

    private TokenManager.onTokenChangeListener validaTokenChange = new TokenManager.onTokenChangeListener(){
        public void tokenChanged(String token){
            validateSubmit();
        }
    };

    private TokenManager.onTokenChangeListener loginTokenChange = new TokenManager.onTokenChangeListener(){
        public void tokenChanged(String token){
            loginSubmit();
        }
    };
    private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                mBtnLogin.setEnabled(true);
            }else{
                mBtnLogin.setEnabled(false);
            }
        }
    };


    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            mBtnValidate.setText("获取验证码");
            mBtnValidate.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            mBtnValidate.setClickable(false);//防止重复点击
            mBtnValidate.setText(millisUntilFinished / 1000 + "s");
        }
    }
}

